package com.foreknow.view;

import com.foreknow.bean.Guestbook;
import com.foreknow.service.impl.GuestbookServiceImpl;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class MainWindow extends JFrame {

    private JPanel contentPane;
    private JTable table;
    //建立一个模型
    DefaultTableModel dtm = null;
    private Guestbook guestbook;

    public JTable getTable() {
        return table;
    }

    public void setTable(JTable table) {
        this.table = table;
    }



    public Guestbook getGuestbook() {
        return guestbook;
    }

    public void setGuestbook(Guestbook guestbook) {
        this.guestbook = guestbook;
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainWindow frame = new MainWindow();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MainWindow() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setForeground(Color.BLACK);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel label = new JLabel("\u540E\u53F0\u7BA1\u7406\u7CFB\u7EDF");
        label.setForeground(Color.GRAY);
        label.setFont(new Font("微软雅黑", Font.BOLD, 12));
        label.setBounds(0, 0, 109, 22);
        contentPane.add(label);
//表头
        String[] columnNames =
                { "id", "name", "phone", "email", "title", "content", "createdtime" };
//内容
        table = new JTable();
        GuestbookServiceImpl guestbookService = new GuestbookServiceImpl();
        List<Object> list = guestbookService.allquery();
        Object[][] obj = new Object[list.size()][7];
        for (int i = 0; i < list.size(); i++)//行
        {
            Guestbook guestbook = (Guestbook) list.get(i);
            for (int j = 0; j < 7; j++)//列
            {
                switch (j)
                {
                    case 0:
                        obj[i][j] = guestbook.getId();
                        break;
                    case 1:
                        obj[i][j] = guestbook.getName();
                        break;
                    case 2:
                        obj[i][j] = guestbook.getPhone();
                        break;
                    case 3:
                        obj[i][j] = guestbook.getEmail();
                        break;
                    case 4:
                        obj[i][j] = guestbook.getTitle();
                        break;
                    case 5:
                        obj[i][j] = guestbook.getContent();
                        break;
                    case 6:
                        obj[i][j] = guestbook.getCreatedtime();
                        break;
                }
            }
        }
        //将表头和数据放入dtm模型
        dtm = new DefaultTableModel(obj,columnNames);
        //用dtm模型建表
        table = new JTable(dtm);

        table.setBounds(10, 64, 401, 149);
        contentPane.add(table);
/**
 * 查询
 */
        JButton btnNewButton = new JButton("\u67E5\u8BE2");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                //select
                Select select =new Select();
                MainWindow.this.dispose();
            }
        });
        btnNewButton.setBounds(131, 21, 93, 23);
        contentPane.add(btnNewButton);
/**
 * 添加
 */
        JButton button = new JButton("\u6DFB\u52A0");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //insert
                Insert insert = new Insert();
                MainWindow.this.dispose();
            }
        });
        button.setBounds(16, 228, 93, 23);
        contentPane.add(button);
/**
 * 修改
 */
        JButton button_1 = new JButton("\u4FEE\u6539");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //update
//                q = table.getSelectedRow();
//                w = table.getSelectedColumn();
//                Object o = table.getValueAt(i,j);
//                Integer a = (Integer) o;
//                //选中的object转int  id;根据id查询;
//                Guestbook guestbook1 = guestbookService.query(a);
//                MainWindow mainWindow = new MainWindow();
//                mainWindow.setGuestbook(guestbook1);
//                Update update = new Update();
                int a = table.getSelectedRow();
                int b = table.getSelectedColumn();
                Object o = table.getValueAt(a,b);
                Integer i = (Integer) o;
                Guestbook guestbook = new Guestbook();
                guestbook.setId(i);
                for (int j =1 ;j<7;j++){
                    Object oo = table.getValueAt(a,j);
                    switch (j){
                        case 1:
                            guestbook.setName((String) oo);
                            break;
                        case 2:
                            guestbook.setPhone((String) oo);
                            break;
                        case 3:
                            guestbook.setEmail((String) oo);
                            break;
                        case 4:
                            guestbook.setTitle((String) oo);
                            break;
                        case 5:
                            guestbook.setContent((String) oo);
                            break;
                        case 6:
                            guestbook.setCreatedtime((String) oo);
                            break;
                    }
                }

                int h = guestbookService.update(guestbook);
                System.out.println(h);
            }
        });
        button_1.setBounds(131, 228, 93, 23);
        contentPane.add(button_1);
/**
 * 删除
 */
        JButton button_2 = new JButton("\u5220\u9664");
        button_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //delete
                //获取选中的单元格内容
                int i = table.getSelectedRow();
                int j = table.getSelectedColumn();
                //获取选中的对象
                Object o = table.getValueAt(i,j);
                Integer a = (Integer) o;
//                int a = table.getSelectedRow();
//                int b = table.getSelectedColumnCount();
//                //获取选中行的对象
//                Object o = table.getValueAt(a,b);
//                //System.out.println(table.getValueAt(a,b));
                //Object竟然还可以强转String------哇偶,酷
//                String key = (String) o;
//                Guestbook guestbook = (Guestbook) o;
//                //获取选中行id,调用方法,根据id删除
                int b = guestbookService.delete(a);
                System.out.println(b);
                //返回所选行的行长度
                int n = table.getSelectedRows().length;
                for (int k=0;k<n;k++){
                    //调用DefaultTableModel中的removeRow移除模型中 row 位置的行。
                    dtm.removeRow(table.getSelectedRow());
                }
            }
        });
        button_2.setBounds(250, 228, 93, 23);
        contentPane.add(button_2);
  /**
   * 模糊查询
   * */
        JButton button_3 = new JButton("\u6A21\u7CCA\u67E5\u8BE2");
        button_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //mohu
                Mohuselect mohuselect = new Mohuselect();
                MainWindow.this.dispose();
            }
        });
        button_3.setBounds(10, 21, 93, 23);
        contentPane.add(button_3);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        this.pack();
    }
}
